<template>
  <div>
    <PageDialog
      :title="dialogInfo.title"
      v-model:visible="shows"
      :width="dialogInfo.width"
      :bt-loading="dialogInfo.btLoading"
      :bt-list="dialogInfo.btList"
    >
      <!-- 条件栏 -->
      <PageFilter
        v-model:query="filterInfo.query"
        :filter-list="filterInfo.list"
      />
      <div class="interface-select">
        <!-- 系统列表 -->
        <DialogTable
          :refresh="tableInfo.refresh"
          :init-curpage="tableInfo.initCurpage"
          v-model:data="tableInfo.data"
          :api="api.system"
          :listen-height="false"
          v-model:query="filterInfo.query"
          :field-list="tableInfo.fieldList"
          :handle="tableInfo.handle"
          :highlight-row="highlightRow"
          :check-box="checkBox"
          :checkedList="checkBox ? checkedList : []"
          :tabIndex="tabIndex"
          @handleEvent="handleEvent"
          :pager="false"
        />
        <!-- 接口列表 -->
        <DialogTable
          :refresh="tableInterfaceInfo.refresh"
          :init-curpage="tableInterfaceInfo.initCurpage"
          v-model:data="tableInterfaceInfo.data"
          :api="api.interface"
          :listen-height="false"
          v-model:query="interfaceFilterInfo.query"
          :field-list="tableInterfaceInfo.fieldList"
          :handle="tableInterfaceInfo.handle"
          :highlight-row="highlightRow"
          :check-box="checkBox"
          :checkedList="checkBox ? checkedList : []"
          :tabIndex="tabIndex"
          @handleEvent="interfaceHandleEvent"
          :pager="false"
        />
      </div>
    </PageDialog>
  </div>
</template>

<script setup name="selectUser">
const { proxy } = getCurrentInstance()
const props = defineProps({
  highlightRow: {
    type: Boolean,
    default: false,
  },
  checkBox: {
    type: Boolean,
    default: true,
  },
  tabIndex: {
    type: Boolean,
    default: true,
  },
  api: {
    type: Object,
  },
  showDialog: {
    type: Boolean,
  },
  query: {
    type: Object,
    default: function () {
      return {}
    },
  },
  checkedList: {
    type: Array,
    default: function () {
      return []
    },
  },
  saveEmpty: {
    type: Boolean,
    default: false,
  },
})
// 过滤相关配置
const filterInfo = ref({
  query: {
    sort: 'create_time desc',
    isEnable: 1,
  },
  list: [
    // {
    //   type: "input",
    //   label: "名称",
    //   value: "nameLike",
    //   event: search,
    // },
    // {
    //   label: "部门",
    //   value: "deptId",
    //   query: {
    //     status: 1,
    //   },
    //   api: getDeptAll,
    //   type: "slip-select",
    //   event: search,
    // },
    // { type: "button", label: "搜索", event: search },
  ],
})
const interfaceFilterInfo = ref({
  query: {
    sort: 'create_time desc',
  },
  list: [
    // {
    //   type: "input",
    //   label: "名称",
    //   value: "nameLike",
    //   event: search,
    // },
    // {
    //   label: "部门",
    //   value: "deptId",
    //   query: {
    //     status: 1,
    //   },
    //   api: getDeptAll,
    //   type: "slip-select",
    //   event: search,
    // },
    // { type: "button", label: "搜索", event: search },
  ],
})
// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  fieldList: [
    { label: '客户端ID', value: 'clientId' },
    { label: '客户端名称', value: 'name' },
    { label: '接口url', value: 'uri' },
  ],
})
const tableInterfaceInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  fieldList: [
    { label: '接口描述', value: 'name' },
    { label: '接口url', value: 'uri' },
    { label: '调用方法', value: 'method' },
  ],
})
// 弹窗相关
const dialogInfo = ref({
  title: '选择接口',
  btLoading: false,
  width: '920px',
  btList: [
    { label: '关闭', event: handleClose, show: true },
    {
      label: '保存',
      type: 'primary',
      disabled: !props.saveEmpty,
      event: save,
      show: true,
    },
  ],
})
const selectRow = ref('')
const shows = computed({
  get() {
    return props.showDialog
  },
  set(val) {
    proxy.$emit('update:showDialog', val)
  },
})
// 获取列表
function getList() {
  tableInfo.value.refresh = Math.random()
}
function search() {
  tableInfo.value.initCurpage = Math.random()
  tableInfo.value.refresh = Math.random()
}
function save() {
  handleClose()
  proxy.$emit('callback', selectRow.value)
}
// 关闭弹窗前的回调
function handleClose() {
  shows.value = false
  proxy.$emit('reset')
}
function interfaceHandleEvent(event, data) {
  switch (event) {
    case 'tableClickRow':
      selectRow.value = data
      if (!props.saveEmpty) {
        dialogInfo.value.btList[1].disabled = !selectRow.value
      }
      break
    case 'tableCheck':
      selectRow.value = data
      if (!props.saveEmpty) {
        dialogInfo.value.btList[1].disabled = !selectRow.value.length
      }
      break
  }
}
function handleEvent(event, data) {
  interfaceFilterInfo.value.query['thirdSystemId'] = data?.id
  tableInterfaceInfo.value.refresh = Math.random()
}
onMounted(() => {
  // hack, 在table创建出来之后再去调用
  proxy.$nextTick(() => {
    getList()
  })
})
</script>

<style scoped lang="scss">
.interface-select {
  display: flex;
  .TableContainer:first-child {
    margin-right: 20px;
  }
}
</style>
